<?php

namespace App\Listeners;

use App\Events\MasterCashed;
use App\Events\OrderFinished;
use App\Models\MasterCash;
use App\Models\Order;
use App\Notifications\OrderNotifications\OrderFinishedNotification;
use App\Services\ISystemTaskService;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class MasterCashedListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = 'finance';

    public $tries = 3;

    public $timeout = 30;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderFinished  $event
     * @return void
     */
    public function handle(MasterCashed $event)
    {
        $cash = MasterCash::find($event->cash_id);
        if(!$cash){
            throw new \Exception('miss cash');
        }

        $ret = resolve(ISystemTaskService::class)->masterCashPay($cash->id);
        if (!$ret){
            throw new \Exception('master cash pay fail');
        }
    }

    public function failed(MasterCashed $event, \Exception $exception)
    {
        //
    }
}
